package com.fadadianqi.logistics.customer.dao.ibatis;

import com.fadadianqi.commons.GenericDAOSuport;
import com.fadadianqi.logistics.customer.dao.AccountDAO;
import com.fadadianqi.logistics.customer.model.Account;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.Pagination.Order;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AccountDAOImpl extends GenericDAOSuport<String, Account> implements AccountDAO {

    @Override
    protected Class<?> getObjectClass() {
        return Account.class;
    }

    @Override
    protected String getTableName() {
        return "account";
    }

    @Override
    public List<Account> findByCustomerId(String customerId,
                                          Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("customerId", customerId);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

    @Override
    public List<Account> findByBillId(String billId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("billId", billId);
        return super.findByPropertys(map);
    }

    @Override
    public int countByCustomerId(String customerId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("customerId", customerId);
        return super.countAll(map);
    }

    @Override
    public List<Account> findByCustomerIdBetween(String customerId, Date b, Date e,
                                                 Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("b", b);
        map.put("e", e);
        map.put("customerId", customerId);
        map.put("pagination", pagination);
        if (pagination != null) {
            map.put(START_ROW_NUM, pagination.getStartRows());
            map.put(MAX_ROW_NUM, pagination.getMaxResults());
            map.put(END_ROW_NUM, pagination.getStartRows() + pagination.getMaxResults());
            List<Order> orderList = pagination.getOrderList();
            if (ValidatorHelper.isNotEmpty(orderList)) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < orderList.size(); i++) {
                    Order order = orderList.get(i);
                    sb.append(order.getName());
                    if (order.getAscending()) {
                        sb.append(" ASC");
                    } else {
                        sb.append(" DESC");
                    }
                    if (i != (orderList.size() - 1)) {
                        sb.append(",");
                    }
                }
                map.put(ORDER_STR, sb.toString());
            }
        }
        return super.getSqlMapClientTemplate().queryForList(getNamespace() + ".findByCustomerIdBetween", map);
    }

    @Override
    public List<Account> getBalanceEnd(String customerId, Date e) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("e", e);
        map.put("customerId", customerId);
        return super.getSqlMapClientTemplate().queryForList(getNamespace() + ".getBalanceEnd", map);
    }

}
